/*
 * http://www.apache.org/licenses/LICENSE-2.0
 * Copyright (c) 2021 fullset
 * @Description:
 * @author: pangps
 * @date: 2021/6/29 20:03
 */
package com.fs.base.config;

import com.fs.core.exception.BizException;
import com.fs.core.exception.BizExceptionEnum;
import com.fs.core.web.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 业务异常handler
     * @param e 异常对象
     * @return 包装后的异常信息
     */
    @ExceptionHandler(BizException.class)
    public BaseResult<?> exceptionHandler(BizException e) {
        BizExceptionEnum exceptionEnum = e.getBizExceptionEnum();
        logger.error("business error", e);
        e.printStackTrace();
        return BaseResult.error(exceptionEnum.getErrorCode(), exceptionEnum.getErrorMsg());
    }
}

